﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TimeAlert
{
    public partial class SettingForm : Form
    {
        private int count = 0;
        public SettingForm()
        {
            InitializeComponent();

        }

        private void btnAddExam_Click(object sender, EventArgs e)
        {
            string name = this.tbName.Text;

            Panel gbox = new Panel();
            gbox.Name = name + "_exam_" + DateTimeOffset.Now.ToUnixTimeMilliseconds();
            gbox.Text = name;
            gbox.Width = 800;
            gbox.Height = 230;
            gbox.AutoScroll = true;
            gbox.BorderStyle = BorderStyle.FixedSingle;
            gbox.Location = new Point(10, 5 + count++ * 150);

            Panel gbox1 = new Panel();
            gbox1.Visible = false;
            gbox1.Name = name + "_tbox_" + DateTimeOffset.Now.ToUnixTimeMilliseconds();
            var t = new Button() { Visible = false, Text = "", Name = gbox1.Name + "_t_" + DateTimeOffset.Now.ToUnixTimeMilliseconds() };
            gbox1.Controls.Add(t);
            gbox.Controls.Add(gbox1);
            this.panel1.Controls.Add(gbox);
            // 
            //TextBox txt = new TextBox();
            //txt.Name = gbox.Name + 0;
            //txt.Location = new Point(12, 15 + 0 * 30);
            //gbox.Controls.Add(txt);

            //Button btn = new Button();
            //btn.Text = "添加阶段";
            //btn.Name = gbox.Name + "_stage";
            //btn.Click += AddStage;
            //btn.Location = new Point(200, 15);
            //gbox.Controls.Add(btn);
            AddStage(t, e);
        }
        public void AddStage(object sender, EventArgs e)
        {
            var btn = sender as Button;
            var p = btn.Parent.Parent;

            int rows = 0;
            for (int i = 0; i < p.Controls.Count; i++)
            {
                if (p.Controls[i].Name.Contains("_box_"))
                {
                    rows++;
                    //  MessageBox.Show(p.Controls[i].Location.Y.ToString());
                }
            }

            long unix = DateTimeOffset.Now.ToUnixTimeMilliseconds();
            int height = 20 + rows * 30;

            int w = 10;
            int s = 20;
            Panel box = new Panel();
            box.Name = p.Name + "_box_" + unix;
            box.Width = 780;
            box.Location = new Point(10, height - (p as Panel).VerticalScroll.Value);
            box.BorderStyle = BorderStyle.None;
            Button btnA = new Button();
            btnA.Text = "+";
            btnA.Width = 18;
            btnA.Click += AddStage;
            btnA.Name = p.Name + "_btnA_" + unix;
            btnA.Location = new Point(w, 0);
            box.Controls.Add(btnA);

            Button btnD = new Button();
            btnD.Text = "-";
            btnD.Width = 18;
            btnD.Click += RemoveStage;
            btnD.Name = p.Name + "_btnA_" + unix;
            btnD.Visible = rows >= 1;
            btnD.Location = new Point(w += btnA.Width, 0);
            box.Controls.Add(btnD);

            Label label = new Label();
            label.Text = "阶段名称";
            label.Name = p.Name + "_lbl_" + unix;
            label.Location = new Point(w += 0 + 18, 0);
            box.Controls.Add(label);

            TextBox tb = new TextBox();
            tb.Name = p.Name + "_tb_" + unix;
            tb.Width = 300;
            tb.Location = new Point(w += 0 + label.Width, 0);
            box.Controls.Add(tb);

            Panel gb = new Panel();
            RadioButton r1 = new RadioButton();
            r1.Text = "考试开始";
            r1.Name = p.Name + "_r1_" + unix;
            r1.Location = new Point(0, 0);
            gb.Controls.Add(r1);
            RadioButton r2 = new RadioButton();
            r2.Text = "考试结束";
            r2.Name = p.Name + "_r2_" + unix;
            r2.Location = new Point(r1.Width, 0);
            gb.Controls.Add(r2);
            gb.BorderStyle = BorderStyle.None;
            gb.Height = r2.Height;
            gb.Location = new Point(w += s + tb.Width, 0);
            gb.Name = p.Name + "_radiobox_" + unix;
            box.Height = gb.Height;
            box.Controls.Add(gb);

            p.Controls.Add(box);

        }

        public void RemoveStage(object sender, EventArgs e)
        {

        }
    }
}
